package org.yn.mkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.yn.aftermarket.domain.Customer;
import org.yn.aftermarket.mapper.CustomerMapper;
import org.yn.emp.domain.Employee;
import org.yn.finance.domain.Deposit;
import org.yn.finance.mapper.DepositMapper;
import org.yn.mkt.domain.Business;
import org.yn.mkt.domain.BusinessRemark;
import org.yn.mkt.domain.Clue;
import org.yn.mkt.dto.DepositDTO;
import org.yn.mkt.mapper.BusinessMapper;
import org.yn.mkt.mapper.BusinessRemarkMapper;
import org.yn.mkt.mapper.ClueMapper;
import org.yn.mkt.service.IBusinessService;
import org.yn.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.yn.order.domain.Contract;
import org.yn.order.domain.ContractRemark;
import org.yn.order.domain.Order;
import org.yn.order.domain.OrderProduct;
import org.yn.order.mapper.ContractMapper;
import org.yn.order.mapper.ContractRemarkMapper;
import org.yn.order.mapper.OrderMapper;
import org.yn.order.mapper.OrderProductMapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yn
 * @since 2023-07-31
 */
@Service
public class BusinessServiceImpl extends BaseServiceImpl<Business> implements IBusinessService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    BusinessRemarkMapper businessRemarkMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderProductMapper orderProductMapper;

    @Autowired
    ContractMapper contractMapper;

    @Autowired
    ContractRemarkMapper contractRemarkMapper;

    @Autowired
    DepositMapper depositMapper;
    @Override
    public void deposit(DepositDTO depositDTO, Employee loginUser) {
        Clue clue = clueMapper.selectById(depositDTO.getClueId());//根据线索id获取 当前线索的对象

        // 客户表新增(交定金之后就变成客户)
        Customer customer = new Customer();
        customer.setName(clue.getFullName()); //用户名字来自线索
        customer.setTel(clue.getMphone());
        customer.setState(0);
        customer.setInputTime(new Date());
        customer.setCustomersourceId(clue.getSource());
        customer.setSellerId(clue.getOwner().getId());
        customer.setInputuserId(loginUser.getId());
        customerMapper.insert(customer);
        //修改商机表 -- 此商机的状态
        Business business = businessMapper.selectById(depositDTO.getId());
        business.setState(1);
        businessMapper.update(business);

        //商机备注表 --
        BusinessRemark businessRemark = new BusinessRemark();
        businessRemark.setCreateTime(new Date());//添加时间
        businessRemark.setBisinessId(depositDTO.getId());//绑定 商机
        businessRemark.setEditBy(loginUser.getId());

        String content="%s 缴纳定金：%s ,操作人 %s";
        // 备注信息
        businessRemark.setNoteContent(String.format(content,customer.getName(),depositDTO.getDeposit(),loginUser.getUsername()));

        businessRemarkMapper.insert(businessRemark);
        // 新增订单表
        Order order = new Order();
        order.setSn(UUID.randomUUID().toString()); //订单的SN 唯一
        order.setBusinessId(depositDTO.getId());
        order.setBusinessName(business.getName());
        order.setCustomerId(customer.getId());
        order.setCustomerName(customer.getName());
        order.setPrice(business.getProductPrice());
        order.setProductId(business.getProductId());
        order.setProductName(business.getProductName());
        order.setState(0);  //0是付了定金，，1是完成订单
        orderMapper.insert(order);
        // 订单产品表
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setName(business.getProductName());
        orderProduct.setOrderId(order.getId());
        orderProduct.setPrice(business.getProductPrice());
        orderProductMapper.insert(orderProduct);
        //订单活动表


        // 合同表
        Contract contract = new Contract();
        String content1="%s 买了 %s";
        contract.setContent(String.format(content1,customer.getName(),business.getProductName()));

        contract.setCreateTime(new Date());
        contract.setCustomerId(customer.getId());
        contract.setCustomerName(customer.getName());
        BigDecimal deposit = new BigDecimal(depositDTO.getDeposit());
        contract.setDeposit(deposit);
        contract.setOrderId(order.getId());
        contract.setOrderSn(order.getSn());
        contract.setPrice(business.getProductPrice());
        contract.setState(0);
        contractMapper.insert(contract);
        //合同备注表
        ContractRemark contractRemark = new ContractRemark();

        contractRemark.setContractId(contract.getId());
        contractRemark.setCreateBy(loginUser.getId());
        contractRemark.setCreateTime(new Date());
        String content2="%s 和用户 %s 创建了合同 %s";
        contractRemark.setNoteContent(String.format(content2,loginUser.getUsername(),customer.getName(),contract.getSn()));
        contractRemarkMapper.insert(contractRemark);

        //定金表
        Deposit deposit1 = new Deposit();
        deposit1.setBusinessId(business.getId());
        deposit1.setBusinessName(business.getName());
        deposit1.setCustomerId(customer.getId());
        deposit1.setCustomerName(customer.getName());
        deposit1.setDeposit(depositDTO.getDeposit().toString());
        deposit1.setName("产品的定金");
        deposit1.setOrderId(order.getId());
        deposit1.setOrderSn(order.getSn());
        deposit1.setPayTime(new Date());
        deposit1.setProductId(business.getProductId());
        deposit1.setProductName(business.getProductName());
        deposit1.setState(0);
        depositMapper.insert(deposit1);


    }

    @Override
    public void moveIntoPool(Long id, Employee loginUser) {
        // 根据id获取这个商机
        Business business = businessMapper.selectById(id);

        //修改状态
        business.setState(-1);  //-1为在商机池中
        businessMapper.update(business);

        // 添加备注
        BusinessRemark businessRemark = new BusinessRemark();

        String content3="%s 将商机 %s 移入商机池";
        businessRemark.setNoteContent(String.format(content3,loginUser.getUsername(),business.getName()));
        businessRemark.setEditBy(loginUser.getId());
        businessRemark.setCreateTime(new Date());
        businessRemarkMapper.insert(businessRemark);

    }

    @Override
    public void moveOutPool(Long id, Employee loginUser) {
        // 根据id获取这个商机
        Business business = businessMapper.selectById(id);

        //修改状态
        business.setState(0);  //0.为在跟进中
        businessMapper.update(business);

        // 添加备注
        BusinessRemark businessRemark = new BusinessRemark();

        String content3="%s 将商机 %s 移出商机池";
        businessRemark.setNoteContent(String.format(content3,loginUser.getUsername(),business.getName()));
        businessRemark.setEditBy(loginUser.getId());
        businessRemark.setCreateTime(new Date());
        businessRemarkMapper.insert(businessRemark);
    }
}
